<?php
/**
 * mysql数据库架构处理类
 *
 * @author Mo yi <root@imoi.cn>
 * @link   http://www.imoi.cn
 * @copyright Copyright &copy; 2010-2012 DuoLam Software LLC
 * @license http://www.imoi.cn/license/
 * @version $Id: dpMysqlSchema.php 2799 2012-04-10 09:31:13Z Mo.yi $
 * @package db.schema
 * @since 1.0
 */

class dpMysqlSchema extends dpSchema implements ISchema
{
    /**
     * 数据表主键
     */
    protected $_pk;

    /** 
     * 字段类型
     */
    public $columnType = array
    (
        'pk'       => 'int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY',
        'string'   => 'varchar(255)',
        'text'     => 'text',
        'integer'  => 'int(11)',
        'int_not'  => 'int(11) NOT NULL DEFAULT 0',
        'float'    => 'float',
        'time'     => 'time',
        'date'     => 'date',
        'datetime' => 'datetime',
        'binary'   => 'blob',
        'boolean'  => 'tinyint(1)',
        'bool_not' => 'tinyint(1) NOT NULL DEFAULT 0',
    );

    /**
     * 获取表字段信息
     * @param string $table 表名
     * @return array  字段信息列表
     */
    public function getColumn($table)
    {
        $caching = (int)D::$config['component']['db']['schema_caching'];
        $rs = $this->connect->finds('SHOW COLUMNS FROM `' . $table . '`', $caching);

        if (empty($rs))
            return false;
        else {
            foreach ($rs as $k => $v) {
                $columns[$k]['name']    = $v['Field'];
                $columns[$k]['default'] = $v['Default'];
                $columns[$k]['type']    = $v['Type'];
                
                if ($v['Key'] == 'PRI') {
                    $columns[$k]['pk']  = true;
                    $columns[$k]['auto']= $v['Extra'] == 'auto_increment';
                    $this->_pk = $v['Field'];
                }
            }
            return $columns;
        }
    }

    /**
     * 获取表主键名称
     * @return string 主键名字
     */
    public function getPk()
    {
        return !empty($this->_pk) ? $this->_pk : false;
    }

    /**
     * 获取重命名表的SQL语句
     * @param string $name    表名称
     * @param string $newName 表新名称
     * @return string 返回重命名表的SQL语句
     */
    public function getRenameTableString($name, $newName)
    {
        return 'RENAME TABLE `' . $name . '` TO `' . $newName . '`';
    }

    /**
     * 获取清空数据表的SQL语句
     * @param string $table 表名称
     * @return string 返回清空数据表的SQL语句
     */
    public function getTruncateTable($table)
    {
        return 'TRUNCATE TABLE `' . $table . '`';
    }

    /**
     * 获取更改字段名的SQL语句
     * @param string $table   表名称
     * @param string $name    字段名
     * @param string $newName 新字段名
     * @return string 返回更改字段名的SQL语句
     */
    public function getRenameColumnString($table, $name, $newName)
    {
        $rs = $this->connect->find('SHOW CREATE TABLE `'.$table.'`');
        preg_match('/\`'.$name.'\`\s(.*?)\,/i',$rs['Create Table'],$matches);
        $type = &$matches[1];
        return 'ALTER TABLE `' . $table . '`'
               . ' CHANGE `' . $name . '` `' . $newName . '` '
               . $type;
    }

}